Page({
  data: {
    tabList: [
      { id: 1, name: '咨询师培训' },
      { id: 2, name: '智慧生活' },
      { id: 3, name: '素质教育' },
      { id: 4, name: '身心健康' },
      { id: 5, name: '免费公开课' }
    ],
    currentTab: 1,
    scrollLeft: 0,
    contentList: [],
    isLoading: true,
    currentPage: 1,
    pageSize: 10,
    totalPages: 1,
    hasMore: true
  },

  onLoad() {
    // 加载初始数据
    this.loadContentList();
  },

  // 切换分类
  switchTab(e) {
    const tabId = e.currentTarget.dataset.id;
    if (tabId === this.data.currentTab) return;

    this.setData({
      currentTab: tabId,
      currentPage: 1,
      contentList: [],
      isLoading: true
    });

    // 计算滚动位置，使选中的标签居中
    const index = this.data.tabList.findIndex(item => item.id === tabId);
    const scrollLeft = index * 120 - (wx.getSystemInfoSync().windowWidth - 120) / 2;
    
    this.setData({
      scrollLeft: scrollLeft > 0 ? scrollLeft : 0
    });

    // 加载新分类的数据
    this.loadContentList();
  },

  // 加载内容列表
  loadContentList() {
    const { currentTab, currentPage, pageSize } = this.data;
    
    // 显示加载状态
    this.setData({ isLoading: true });

    // 实际项目中替换为真实API请求
    wx.request({
      url: `http://localhost:8080/fm/page`,
      method: 'GET',
      data: {
        fmClassifyId:currentTab, // 分类ID
        pageNum: currentPage,
        pageSize: pageSize
      },
      success: (res) => {
        if (res.data.code === 200) {
          const contentList = res.data.data.list || [];
          const total = res.data.data.total ;
          const totalPages = res.data.data.pageTotal 
          console.log("获取到的内容列表：", contentList); 
          this.setData({
            contentList: currentPage === 1 ? contentList : [...this.data.contentList, ...contentList],
            totalPages: totalPages,
            hasMore: currentPage < totalPages,
            isLoading: false
          });
        } else {
          wx.showToast({
            title: '加载失败',
            icon: 'none'
          });
          this.setData({ isLoading: false });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
        this.setData({ isLoading: false });
      }
    });
  },

  // 上一页
  prevPage() {
    if (this.data.currentPage <= 1) return;
    
    this.setData({
      currentPage: this.data.currentPage - 1,
      isLoading: true
    });
    
    this.loadContentList();
  },

  // 下一页
  nextPage() {
    if (this.data.currentPage >= this.data.totalPages) return;
    
    this.setData({
      currentPage: this.data.currentPage + 1,
      isLoading: true
    });
    
    this.loadContentList();
  },

  // 格式化时间
  formatTime(timestamp) {
    if (!timestamp) return '未知时间';
    
    // 兼容多种时间格式
    const date = typeof timestamp === 'string' 
      ? new Date(timestamp.replace(/-/g, '/')) 
      : new Date(timestamp);
      
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    
    return `${year}-${month}-${day}`;
  },

  // 导航到详情页
  navigateToDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/fmDetail/fmDetail?id=${id}`
    });
  }
});